package com.cracker.websocket.server.event.listener.sessionOffline;

import com.cracker.websocket.server.event.SessionOfflineEvent;
import com.cracker.websocket.server.service.ChatRoomService;
import com.cracker.websocket.server.service.SessionService;
import com.cracker.websocket.server.service.StatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import javax.websocket.Session;

/**
 * 离线统计
 */
@Component
@Slf4j
public class OfflineStat implements ApplicationListener<SessionOfflineEvent> {
    @Autowired
    private StatService statService;
    @Autowired
    private SessionService sessionService;
    @Autowired
    private ChatRoomService chatRoomService;

    @Override
    public void onApplicationEvent(SessionOfflineEvent sessionOfflineEvent) {
        Session session = sessionOfflineEvent.getSession();
//        UserEntity userEntity = (UserEntity) session.getUserProperties().get("user");
//        ChatRoomEntity chatRoomEntity = (ChatRoomEntity) session.getUserProperties().get("live");
//        chatRoomService.removeUser(userEntity.getId(), chatRoomEntity.getId());
        sessionService.sessionOffline(session);
        statService.decOnlineCount();
        log.warn("用户下线,当前在线人数:{}", statService.getOnlineCount());
    }
}
